package cn.itcast.travel.web.v0;

import cn.itcast.travel.domain.ResultInfo;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.UserService;
import cn.itcast.travel.service.impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
/**
 * 登录功能的实现
 * @author
 * */
//@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //验证码验证操作
        //获取前端输入的数据
        String check = request.getParameter("check");
        HttpSession session = request.getSession();

        String checkCode = (String) session.getAttribute("CHECKCODE_SERVER");
        //防止验证码进行重复使用
        session.removeAttribute("CHECKCODE_SERVER");
        if (!checkCode.equalsIgnoreCase(check)){
            //验证码输入错误
            ResultInfo resultInfo = new ResultInfo();
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("验证码输入有误");
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(resultInfo);
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(json);
            //结束该方法
            return;
        }
        //获取登录信息的集合
        Map<String, String[]> map = request.getParameterMap();
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        UserService userService = new UserServiceImpl();
        User u=userService.login(user);
        ResultInfo resultInfo = new ResultInfo();
        //用户为null，登录失败
        if (u==null){
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("该账号还没有注册，请先注册");
        }
        //用户的激活状态不是Y
        if (u!=null&& !"Y".equals(u.getStatus())){
            resultInfo.setFlag(false);
            resultInfo.setErrorMsg("账号尚未激活，请先激活");
        }
        //激活成功
        if (u!=null && "Y".equals(u.getStatus())){
            resultInfo.setFlag(true);
            //将用户的信息存储session中用于用户信息的展示
            session.setAttribute("login_msg",u);
        }
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(resultInfo);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(json);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
